//
//  TSReceiveMsgEditExtendStepViewController.m
//  chart
//
//  Created by xu on 15/8/23.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSReceiveMsgEditExtendStepViewController.h"
#import "TSToolBarTextField.h"
#import "TSMutipleCheckTableViewController.h"

#define SEARCH_LABEL_WIDTH  60.f
#define SEARCH_LABEL_HEIGHT 35.f
#define SEARCH_ROW_GAP 10.f

@interface TSReceiveMsgEditExtendStepViewController ()

@end

@implementation TSReceiveMsgEditExtendStepViewController{
    TSToolBarTextField *reasonTextFiled;
    TSToolBarTextField *entrustTextField;
    QSFetchModel *actionModel;
}

@synthesize currentAction, msgId;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentViewController:) name:TSNotification_PresentViewController object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMultipleSelectCompelete:) name:TSNotification_OnMultipleSelectCompelete object:nil];
    
    WS(weakSelf);
    [self rightBarButtonWithTitle:@"提交" barNorImage:nil barHltImage:nil action:^{
        [weakSelf sendActionToServer];
    }];
    
    self.title = @"操作消息";
    
    [self addReasonRow];
    
    if (currentAction == Action_Type_Entrust) {
        [self addEntrustRow];
    }
    
    actionModel = [[QSFetchModel alloc] init];
}

- (void)presentViewController:(NSNotification *)noti
{
    TSMutipleCheckTableViewController *multiple_vc = [[TSMutipleCheckTableViewController alloc] init];
    TSBaseNavigationController *nav = [[TSBaseNavigationController alloc] initWithRootViewController:multiple_vc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)onMultipleSelectCompelete:(NSNotification *)noti
{
    NSArray *names = [noti.object objectForKey:TSDictionaryKeyDefine_Name];
    if (entrustTextField) {
        [entrustTextField setText:[self getNamesInArray:names]];
    }
}

- (NSString *)getNamesInArray:(NSArray *)names
{
    NSMutableString *total_names = [NSMutableString stringWithString:@""];
    for (int i=0; i<names.count; i++) {
        TSUserModel *user_model = [names objectAtIndex:i];
        [total_names appendString:user_model.userName];
        
        if (i != names.count-1) {
            [total_names appendString:@","];
        }
    }
    
    return total_names;
}

- (void)sendActionToServer
{
    WS(weakSelf);
    NSString *url;
    if (currentAction == Action_Type_Entrust) {
        url = kRequestUrl_SendMessage_entrust;
        
        actionModel.requestParams = @{TSDictionaryKeyDefine_Id:msgId?:@"", TSDictionaryKeyDefine_Namelist:entrustTextField.text?:@"", TSDictionaryKeyDefine_Reason:reasonTextFiled.text?:@""};
    }
    else if (currentAction == Action_Type_Vacate){
        url = kRequestUrl_SendMessage_leave;
        
        actionModel.requestParams = @{TSDictionaryKeyDefine_Id:msgId?:@"", TSDictionaryKeyDefine_Reason:reasonTextFiled.text?:@""};
    }
    else if (currentAction == Action_Type_Reject){
        url = kRequestUrl_AuditMessage_disagree;
        
        actionModel.requestParams = @{TSDictionaryKeyDefine_Id:msgId?:@"", TSDictionaryKeyDefine_Reason:reasonTextFiled.text?:@""};
    }
    
    [actionModel fetchWithPath:url completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (isSucceeded) {
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_MessageChangeActionSucceeded object:nil];
            
            [[LBToastManager manager] showtoast:@"操作成功"];
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }
    } NoProgress:NO];
}


- (UILabel *)createLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    
    return label;
}

- (void)addReasonRow
{
    NSString *title = @"委托理由:";
    if (currentAction == Action_Type_Vacate) {
        title = @"请假理由:";
    }
    else if (currentAction == Action_Type_Reject){
        title = @"拒绝理由:";
    }
    
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, MARGIN_TOP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:title];
    [self.view addSubview:label];
    
    reasonTextFiled = [[TSToolBarTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), currentDeviceWidth-2*MARGIN_LEFT-SEARCH_LABEL_WIDTH-10.f, SEARCH_LABEL_HEIGHT)];
    reasonTextFiled.layer.borderWidth = 1.f;
    [self.view addSubview:reasonTextFiled];
}

- (void)addEntrustRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(reasonTextFiled.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"委托对象:"];
    [self.view addSubview:label];
    
    entrustTextField = [[TSToolBarTextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(reasonTextFiled.frame), SEARCH_LABEL_HEIGHT)];
    entrustTextField.layer.borderWidth = 1.f;
    entrustTextField.notShowKeyboard = YES;
    [self.view addSubview:entrustTextField];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
